#include <stdint.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h> 
#include <stdlib.h>
#include <assert.h>
#include "app_platform/cntl_info.h"
#include "helloworlddao.h"

#include "c2cplatform/component/dal/dal_set.h"
#include "c2cent/storage/mysqlstorage.h"
//#include "app_platform/db_operator2.h"

using namespace c2cplatform::component::dal;
using namespace c2cent::storage;

using namespace erp::helloworld::dao;

CPP_SELF_DEFINE_START
/**
*  请在这里增加自定义的成员变量和函数及其他内容，否则无法合并代码
**/

CPP_SELF_DEFINE_END

/**
 *构建函数
 */
CHelloWorldDao::CHelloWorldDao(enum DeployMethod method)
{
}

/**
 *析构函数
 */
CHelloWorldDao::~CHelloWorldDao()
{
}


/**
 *每一次服务请求时，container都会调用此方法来清空数据
 */
void  CHelloWorldDao::Reset()
{
}


int  CHelloWorldDao::Initialize(IUserDefineCntl* pCntl)
{
	int iRet = 0;
	 if(CDAL_SET->Initialize() != 0)
   {
        printf("CDAL_SET Init Failed, %s\n", CDAL_SET->getLastErrMsg());
        return -1;
   }
	return iRet;
}


/**
 *
 *  根据ID获取 HelloWorld 信息
 *
 *@param String machineKey: 机器码，必填   [in]
 *@param String source: 调用来源，必填   [in]
 *@param uint32_t sceneId: 场景id，必填   [in]
 *@param HelloWorldFilterDdo helloWorldFilter: 过滤器，必填   [in]
 *@param String inReserve: 请求保留字   [in]
 *@param String errmsg: 错误信息  	[out]
 *@param Map helloWorlds: HelloWorld 信息  	[out]
 *@param String outReserve: 返回保留字   	[out]
 *
 *@return  uint32_t : 0(成功) 其它失败
 *
 */
uint32_t CHelloWorldDao::GetHelloWorldById(
				const CCntlInfo& rCntlInfo,
				const std::string& strMachineKey,
				const std::string& strSource,
				uint32_t dwSceneId,
				const erp::helloworld::ddo::CHelloWorldFilterDdo& oHelloWorldFilter,
				const std::string& strInReserve,
				std::string& strErrmsg,
				std::map<uint32_t,erp::helloworld::ddo::CHelloWorldDdo >& mapHelloWorlds,
				std::string& strOutReserve
	)
{
	uint32_t dwRet = 0;
	//TODO:业务代码
	
	return dwRet;
}

/**
 *
 *  写入HelloWorld信息
 *
 *@param String machineKey: 机器码，必填   [in]
 *@param String source: 调用来源，必填   [in]
 *@param uint32_t sceneId: 场景id，必填   [in]
 *@param HelloWorldDdo helloWorld: HelloWorld 信息，必填   [in]
 *@param String inReserve: 请求保留字   [in]
 *@param String errmsg: 错误信息  	[out]
 *@param HelloWorldDdo helloWorldBack: HelloWorld 信息，必填  	[out]
 *@param String outReserve: 返回保留字   	[out]
 *
 *@return  uint32_t : 0(成功) 其它失败
 *
 */
uint32_t CHelloWorldDao::AddHelloWorld(
				const CCntlInfo& rCntlInfo,
				const std::string& strMachineKey,
				const std::string& strSource,
				uint32_t dwSceneId,
				const erp::helloworld::ddo::CHelloWorldDdo& oHelloWorld,
				const std::string& strInReserve,
				std::string& strErrmsg,
				erp::helloworld::ddo::CHelloWorldDdo& oHelloWorldBack,
				std::string& strOutReserve
	)
{
	uint32_t dwRet = 0;
	//TODO:业务代码
/*
	uint16_t iSetId = 0;
	uint32_t dwTime = 0;
	m_dwOperatorUin=rCntlInfo.getOperatorUin();
    m_dwSceneId=dwSceneId;
    m_strMachineKey=strMachineKey;
    m_strSource=strSource;

    C2C_WW_LOG("AddHelloWorld %s OpUin:%u MachineKey:%s Source:%s SceneId:%u", __FUNCTION__,
                    m_dwOperatorUin, strMachineKey.c_str(), strSource.c_str(), dwSceneId);

    //打印性能日志的同时进行接口的itil上报
    //C2C_TIME_ELF_ITIL oWTimeElf(C2C_WWLOG,__FUNCTION__,&dwRet,&m_oGetEmployee_Group);

    c2cent::storage::CMySqlStorage oStorage(CDAL_SET->GetDBOperator(rCntlInfo.getCmdId(),
    		                                rCntlInfo.getOperatorUin(), DB_ROLE_NAME_Employee, "t_employee",iSetId, true));
    std::stringstream sql;

    sql<<"select FEmployeeId,FEmployeeName,FEmployeeEnglishName,FEmployeeStatus,FEmployeeProperty,";
    sql<<" FEmployeeType,FEmployeeSex,FEmployeeAddTime,FEmployeeLastUpdateTime from";
    sql << " " << CDAL_SET->GetTableName(iSetId, 0, DB_ROLE_NAME_Employee, "t_employee").c_str();

    sql<<" where 1=1";

    sql<<" limit "<<(oEmployeeFilter.GetPage()-1)*oEmployeeFilter.GetPageSize()<<","<<oEmployeeFilter.GetPageSize();

    IResultSet& oRs = oStorage.QueryForResultSet(sql.str());


    if(!oRs.IsReady()) //db操作错误
     {
    	oRs.Close();
    	C2C_WW_LOG_ERR(ERR_EMPLOYEE_GET_FAIL, "ExeSql Failed, Sql:%s", sql.str().c_str());
        C2C_WW_LOG_ERR(ERR_EMPLOYEE_GET_FAIL, "ExeSql Failed, OpUin:%u MachineKey:%s Source:%s SceneId:%u",
                            rCntlInfo.getOperatorUin(), strMachineKey.c_str(), strSource.c_str(), dwSceneId);
        return ERR_EMPLOYEE_GET_FAIL;
      }

      if(!oRs.First() || oRs.GetRecordCount() == 0) //无记录
      {
    	  oRs.Close();
          C2C_WW_LOG_ERR(ERR_EMPLOYEE_GET_NONE, "ExeSql Failed, Sql:%s", sql.str().c_str());
          C2C_WW_LOG_ERR(ERR_EMPLOYEE_GET_NONE, "QuerySql not exist, "
                            "OpUin:%u MachineKey:%s Source:%s SceneId:%u",
                            rCntlInfo.getOperatorUin(), strMachineKey.c_str(), strSource.c_str(), dwSceneId);
          return ERR_EMPLOYEE_GET_NONE;
      }

      do{
    	  erp::employee::ddo::CEmployeeDdo oEmployeeDdo;
    	  oEmployeeDdo.SetEmployeeId((uint32_t)oRs.GetUInt("FEmployeeId"));
    	  oEmployeeDdo.SetEmployeeName(oRs.GetString("FEmployeeName")?oRs.GetString("FEmployeeName"):"");
    	  oEmployeeDdo.SetEmployeeEnglishName(oRs.GetString("FEmployeeEnglishName")?oRs.GetString("FEmployeeEnglishName"):"");
    	  oEmployeeDdo.SetEmployeeStatus((uint16_t)oRs.GetUInt("FEmployeeStatus"));
          uint64_t ddwProperty=(uint64_t)oRs.GetBigUInt("FEmployeeProperty");
          oEmployeeDdo.SetEmployeeProperty(ddwProperty);
          oEmployeeDdo.SetEmployeeType((uint16_t)oRs.GetUInt("FEmployeeType"));
          
          oEmployeeDdo.SetEmployeeSex((uint8_t)oRs.GetUInt("FEmployeeSex"));
          mapEmployees[(uint32_t)oRs.GetUInt("FEmployeeId")] = oEmployeeDdo;
        }while(oRs.Next());
        oRs.Close();
	*/
	return dwRet;
}
